﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Windows.Forms;
using System.Collections;




public partial class CreateTaskAdmin : System.Web.UI.Page
{
    
    private void LoadSubject()
    {
        ddlSub.Items.Clear();
        DataTable dtSubjects = new DataTable();
        dtSubjects = AdminCreateTestDAL.GetSubjects();
        ddlSub.Items.Add("--Select One--");
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("--Select One--"));
        for (int i = 0; i < dtSubjects.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dtSubjects.Rows[i][0].ToString();
            li.Value = dtSubjects.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }

    }

    public static int taskid;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            //Clear();
            LoadSubject();
            LoadOwner();
            LoadTestName();
            LoadTaskType();
            
           taskid = Convert.ToInt32(Request.QueryString["TaskId"]);
            
            if(taskid!=0)
            {
                LoadValues();
               
               StudentInfo user;
               DataTable dtUsers = new DataTable();
                dtUsers = DALTask.GetUsersRelatedtoTaskId(taskid, 5);
                UserSelection.sObj.Clear();
                for (int i = 0; i < dtUsers.Rows.Count; i++)
                {

                    user = new StudentInfo(Int32.Parse(dtUsers.Rows[i][0].ToString()), dtUsers.Rows[i][1].ToString());

                    UserSelection.sObj.Add(user);
                    //lblMessage.Text = "Total User Selected: " + UserSelection.sObj.Count.ToString();
                    
                    
                }
            }
           
        }
    }

    
    private void LoadOwner()
    {
        ddlTaskOwner.Items.Clear();
        DataTable dtTaskOwner = new DataTable();
        dtTaskOwner = DALTask.GetOwner();
        ddlTaskOwner.Items.Add("--Select One--");
        ddlTaskOwner.SelectedIndex = ddlTaskOwner.Items.IndexOf(ddlTaskOwner.Items.FindByText("--Select One--"));
        for (int i = 0; i < dtTaskOwner.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            //string  Name = dtTaskOwner.Rows[i][1].ToString() + "  "+ dtTaskOwner.Rows[i][2].ToString();
            string Name = dtTaskOwner.Rows[i][1].ToString();

            li.Text = Name;
            li.Value = dtTaskOwner.Rows[i][0].ToString();
            ddlTaskOwner.Items.Add(li);
        }

    }
    private void LoadValues()
    {
        DataTable dtTask = new DataTable();
        dtTask = DALTask.DisplayTask(taskid);
        txtTaskName.Text = dtTask.Rows[0][0].ToString();
        txtTaskDesc.Text = dtTask.Rows[0][1].ToString();
        ddlTaskType.SelectedItem.Selected = false;
        ddlTaskType.Items.FindByValue(dtTask.Rows[0][2].ToString()).Selected = true;
        ddlTaskOwner.SelectedItem.Selected = false;
        ddlTaskOwner.Items.FindByValue(dtTask.Rows[0][3].ToString()).Selected = true;
        ddlTestName.SelectedItem.Selected = false;
        ddlTestName.Items.FindByValue(dtTask.Rows[0][4].ToString()).Selected = true;
        txtStartDate.Text = dtTask.Rows[0][7].ToString();
        txtEndDate.Text = dtTask.Rows[0][8].ToString();
        if (Int32.Parse(ddlTaskType.SelectedItem.Value) == 14)
        {
            pnlSubject.Visible = true;

            ddlSub.SelectedItem.Selected = false;
            ddlSub.Items.FindByValue(dtTask.Rows[0][5].ToString()).Selected = true;
            DataTable dtTopicsAvailable = new DataTable();
            dtTopicsAvailable = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));

            for (int i = 0; i < dtTopicsAvailable.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dtTopicsAvailable.Rows[i][0].ToString();
                li.Value = dtTopicsAvailable.Rows[i][1].ToString();

                lstTopics.Items.Add(li);
            }
            DataTable dtTopicsSelected;

            dtTopicsSelected = DALTask.GetTopicBasedOnTaskId(dtTask.Rows[0][6].ToString());

            for (int i = 0; i < dtTopicsSelected.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dtTopicsSelected.Rows[i][1].ToString();
                li.Value = dtTopicsSelected.Rows[i][0].ToString();
                lstTopicSelect.Items.Add(li);

            }
        }
        
        else
        {
            pnlSubject.Visible = false;
        }
        ddlStatus.SelectedItem.Selected = false;
        ddlStatus.Items.FindByValue(dtTask.Rows[0][9].ToString()).Selected = true;
        //txtRemarks.Text = dtStatus.Rows[0][1].ToString();
        
    }
    private  void LoadTaskType()
    {
        ddlTaskType.Items.Clear();
        DataTable dtTaskType = new DataTable();
        dtTaskType = DALTask.GetTaskType();
        ddlTaskType.Items.Add("--Select One--");
        ddlTaskType.SelectedIndex = ddlTaskType.Items.IndexOf(ddlTaskType.Items.FindByText("--Select One--"));
        for (int i = 0; i < dtTaskType.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dtTaskType.Rows[i][1].ToString();
            li.Value = dtTaskType.Rows[i][0].ToString();
            ddlTaskType.Items.Add(li);
        }

    }

    private void LoadTestName()
    {
        ddlTestName.Items.Clear();
        DataTable dtTestName = new DataTable();
        dtTestName = DALTask.GetTestName();
        ddlTestName.Items.Add("--Select One--");
        ddlTestName.SelectedIndex = ddlTestName.Items.IndexOf(ddlTestName.Items.FindByText("--Select One--"));
        for (int i = 0; i < dtTestName.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dtTestName.Rows[i][1].ToString();
            li.Value = dtTestName.Rows[i][0].ToString();
            ddlTestName.Items.Add(li);
        }

    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ListItem lst=new ListItem();
        
        if (lstTopics.Items.Count == 0)
            MessageBox.Show("Topics not present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else if (lstTopics.SelectedIndex == -1)
            MessageBox.Show("First Select One Topic", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else
             lstTopicSelect.Items.Add(new ListItem(lstTopics.SelectedItem.Text,lstTopics.SelectedItem.Value));
        
    }


    public bool Validation()
    {
        lblErrorMessage.ForeColor = System.Drawing.Color.Red;


        if (ddlTaskType.SelectedItem.Text=="--Select One--")
        {

            lblErrorMessage.Text = "Please Select TaskType";
            return false;
        }

        if (txtStartDate.Text == "")
        {

            lblErrorMessage.Text = "Please Select StartDate";
            return false;
        }
        if (txtEndDate.Text == "")
        {

            lblErrorMessage.Text = "Please Select EndDate";
            return false;
        }

        if (ddlTaskType.SelectedItem.Text=="Assignment" && ddlSub.SelectedItem.Text=="--Select One--")
        {

            lblErrorMessage.Text = "Please Select Subject";
            return false;
        }
        if (ddlTaskType.SelectedItem.Text == "Assignment" && lstTopicSelect.Items.Count == 0)
        {
            lblErrorMessage.Text = "Please Select Topics";
            return false;
           
        }
        return true;
    }

      
       

    //}
    
           


    

    protected void btnRemove_Click(object sender, EventArgs e)
    {

        if (lstTopicSelect.Items.Count == 0)
            MessageBox.Show("Topics not present", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else if (lstTopicSelect.SelectedIndex == -1)
            MessageBox.Show("First Select One Topic", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        else
        
            lstTopicSelect.Items.RemoveAt(lstTopicSelect.Items.IndexOf(lstTopicSelect.SelectedItem));

    }
    protected void CalendarDisplay_SelectionChanged(object sender, EventArgs e)
    {
        txtStartDate.Text = CalendarDisplay.SelectedDate.ToShortDateString();
    }
    protected void CalendarEndDt_SelectionChanged(object sender, EventArgs e)
    {
        txtEndDate.Text = cldDisplay.SelectedDate.ToShortDateString();
    }
   
    private void insert()
    {
        int TestName, TaskOwner, TaskType, Status, SubjectId = 0, LibraryId, IsVerify, value;
        String TaskName, TaskDes, Remarks, topic = null, UserId="";
        DateTime StartDate, EndDate;
        TestName = Convert.ToInt32(ddlTestName.SelectedValue);
        TaskOwner = Convert.ToInt32(ddlTaskOwner.SelectedValue);
        TaskType = Convert.ToInt32(ddlTaskType.SelectedValue);
        Status = Convert.ToInt32(ddlStatus.SelectedValue);
        TaskName = txtTaskName.Text;
        TaskDes = txtTaskDesc.Text;
        Remarks = txtRemarks.Text;
        if (ddlTaskType.SelectedItem.Text == "Assignment")
        {
            SubjectId = Convert.ToInt32(ddlSub.SelectedValue);
            for (int i = 0; i < lstTopicSelect.Items.Count; i++)
            {
                topic += lstTopicSelect.Items[i].Value + "|";
            }
        }

       
            for (int i = 0; i < UserSelection.sObj.Count; i++)
            {

                UserId += UserSelection.sObj[i].sid + "|";
            }
            
             UserSelection.sObj.Clear();
            
       
        
            LibraryId = Convert.ToInt32(gvLibrary.SelectedIndex);
            if (chkVerify.Checked == true)
            {
                value = 1;
            }
            else
            {
                value = 0;
            }
            IsVerify = value;

            StartDate = Convert.ToDateTime(txtStartDate.Text);
            EndDate = Convert.ToDateTime(txtEndDate.Text);
        
      
            if (taskid == 0)
            {
                DALTask.SetTask(TaskName, TaskDes, TaskType, TaskOwner, SubjectId, topic, LibraryId, IsVerify, TestName, StartDate, EndDate, UserId, Status, Remarks, 1);
            }
            else
            {
                DALTask.ModifyTask(taskid, TaskName, TaskDes, TaskOwner, SubjectId, topic, LibraryId, IsVerify, TestName, Remarks,TaskType, StartDate, EndDate, UserId, Status, 2);
            }
        
       
    }

   
    
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Clear(); 
        
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       if(Validation() == true)
       {
           
               insert();
               Clear();   
       }
       
    }

    public void Clear()
    {

        
        txtTaskName.Text = "";
        txtTaskDesc.Text = "";
        txtEndDate.Text = "";
        txtStartDate.Text = "";
        ddlSub.SelectedIndex = -1;
        ddlTaskOwner.SelectedIndex = -1;
        ddlTaskType.SelectedIndex = -1;
        ddlTestName.SelectedIndex = -1;
        lstTopicSelect.Items.Clear();
        chkVerify.Checked = false;
        lstTopics.Items.Clear();
        ddlStatus.SelectedIndex = -1;
        txtRemarks.Text = "";
       // userobj = (UserControls_UserSelection)LoadControl("UserControls/UserSelection.ascx");
        //userobj.clear();
    }
   
   

    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {

        lstTopics.Items.Clear();
       
        if (ddlSub.SelectedItem.Text == "--Select One--")
            MessageBox.Show("Please Select One Subject", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
        {
           DataTable dtLoadSubject = new DataTable();
            dtLoadSubject = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            for (int i = 0; i < dtLoadSubject.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dtLoadSubject.Rows[i][0].ToString();
                li.Value = dtLoadSubject.Rows[i][1].ToString();
                lstTopics.Items.Add(li);
            }
        }
    }

    protected void ddlTaskType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Int32.Parse(ddlTaskType.SelectedItem.Value) == 14)
        {
            pnlSubject.Visible = true;
        }
        else
        {
            pnlSubject.Visible = false;
        }
    }

    protected void ddlTaskOwner_SelectedIndexChanged(object sender, EventArgs e)
    {


    }
    
}